package com.gkj.blog.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.service.ResourcesService;
import com.gkj.blog.business.vo.condition.ResourceConditionVO;
import com.gkj.blog.business.vo.entity.Resources;
import com.gkj.blog.framework.object.AbstractServiceImpl;
import com.gkj.blog.persistence.mapper.SysResourcesMapper;
import com.gkj.blog.persistence.po.SysResources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class ResourcesServiceImpl extends AbstractServiceImpl<Resources,SysResources> implements ResourcesService {
    SysResourcesMapper sysResourcesMapper;

    @Autowired
    public void setSysResourcesMapper(SysResourcesMapper sysResourcesMapper) {
        this.sysResourcesMapper = sysResourcesMapper;
        setBaseMapper(sysResourcesMapper);
    }

    @Override
    public PageInfo<Resources> findPageBreakByCondition(ResourceConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysResources> sysResources = sysResourcesMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(sysResources)) {
            return null;
        }
        List<Resources> resources = new ArrayList<>();
        for (SysResources r : sysResources) {
            resources.add(new Resources(r));
        }
        PageInfo bean = new PageInfo<SysResources>(sysResources);
        bean.setList(resources);
        return bean;
    }

    @Override
    public List<Resources> queryResourcesListWithSelected(Long rid) {
        List<SysResources> sysResources = sysResourcesMapper.queryResourcesListWithSelected(rid);
        List<Resources> resources=new LinkedList<>();
        sysResources.forEach((e)->{
            resources.add(new Resources(e));
        });
        return resources;
    }

    @Override
    public List<Resources> listUserResources(Long userId) {
        List<SysResources> sysResources = sysResourcesMapper.listUserResources(userId);
        List<Resources> resources=new LinkedList<>();
        sysResources.forEach((e)->{
            resources.add(new Resources(e));
        });
        return resources;
    }

    @Override
    public List<Resources> listAllParentResource() {
        List<SysResources> sysResources = sysResourcesMapper.listAllParent();
        List<Resources> resources=new LinkedList<>();
        sysResources.forEach((e)->{
            resources.add(new Resources(e));
        });
        return resources;
    }

    @Override
    public List<Resources> listUrlAndPermission() {
        List<SysResources> sysResources = sysResourcesMapper.listUrlAndPermission();
        List<Resources> resources=new LinkedList<>();
        sysResources.forEach((e)->{
            resources.add(new Resources(e));
        });
        return resources;
    }


}
